package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class Practice10HistogramView extends View {

    private Paint mPaint;
    private int[] mArray;

    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mArray = new int[]{1, 3, 2, 5, 6, 7};
    }

    public Practice10HistogramView(Context context) {
        super(context);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画直方图
        int width = getWidth();
        int height = getHeight();
        int margin = 80;
        int itemMargin = 10;
        int itemUnitHeight = (height - margin * 2) / 7;
        int itemUnitWidth = (width - margin * 2 - (mArray.length + 1) * itemMargin) / mArray.length;

        Path path = new Path();
        path.moveTo(margin, margin);
        path.lineTo(margin, height - margin);
        path.lineTo(width - margin, height - margin);

        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawPath(path, mPaint);

        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < mArray.length; i++) {
            canvas.drawRect(margin + (i + 1) * itemMargin + i * itemUnitWidth, mArray[i] * itemUnitHeight, margin + (i + 1) * (itemMargin + itemUnitWidth), height - margin, mPaint);
        }
    }
}
